<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 客服临时任务
 */
class TempTaskController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 用户列表
     */
    public function user_list()
    {
        $this->display();
        die;
    }

    /**
     * 今天还款用户
     */
    public function today_pay()
    {
        // 2今日还款 3明天还款
        $index = I('index');
        //去除掉客服经理负责的用户，客服经理自己负责
        $w = [];
        $w['a.type'] = 2;
        $w['a.contact_status'] = 1;
        $w['b.status'] = 1;
//        $w['b.is_success'] = 2;
//        $manager_user = M('kefu_ownuser', '', 'KEFU')->join('a join dxd_caiwu.user_repayment b on b.vid=a.vid')->where($w)->group('a.vid')->getfield('a.vid');
        $where = [];
        $where['a.status'] = array('notin', array(3, 5));
//        $where['a.vid'] = array('notin', $manager_user);
//        if ($index == '2') {
//            $start=1529424000;
//            $end=1529510399;
        $start = strtotime(date('Y-m-d', time()));
        $end = strtotime(date('Y-m-d 23:59:59', time()));
        $where['a.repay_time'] = array('BETWEEN', array($start, $end));
//        }
//        else if ($index == '3') {
//            $start = strtotime(date('Y-m-d', time())) + 24 * 3600;
//            $end = strtotime(date('Y-m-d 23:59:59', time())) + 24 * 3600;
//            $w=[];
//            $w['c.overdue_num']=array('gt',0);
//            $w['c.borrow_num']=array('lt',6);
//            $w['_logic']='or';
//            $where['_complex']=$w;
//            $where['a.repay_time'] = array('BETWEEN', array($start, $end));
//            $where['_logic']='and';
//        }

        $count = M('user_repayment', '', 'CAIWU_R')->join('a join dxd_user.user b on a.vid=b.vid')->where($where)->field('a.vid')->count();
        $count = intval($count);
        $row = '10';
        $n = I('page');
        $page = ($n - 1) * $row;
        $field = 'a.repay_money,a.type,a.status,a.repay_time,b.mobile,b.truename,b.vid,b.sex';
        $data = M('user_repayment', '', 'CAIWU_R')->join('a join dxd_user.user b on a.vid=b.vid')->field($field)->where($where)->LIMIT($page, $row)->select();
        foreach ($data as $k => $v) {
            if ($v['status'] == 1) {
                $data[$k]['status'] = '待还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '还款中';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '还款成功';
            } else if ($v['status'] == 4) {
                $data[$k]['status'] = '还款失败';
            } else if ($v['status'] == 5) {
                $data[$k]['status'] = '取消还款';
            }

            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }

            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            $data[$k]['repay_time'] = date('Y-m-d H:i', $v['repay_time']);
            $w = [];
            $w['vid'] = $v['vid'];
            $w['ctime'] = array('gt', strtotime(date('Y-m-d', time())));
            $called = M('temp_today_pay_mark', '', 'KEFU')->where($w)->order('id desc')->limit(1)->field('reason')->find();
            //最近备注
            if (!empty($called)) {
                $data[$k]['remark'] = $called['reason'];
            } else {
                $data[$k]['remark'] = '--';
            }
        }
        $count = intval($count);
        $tol = ceil($count / $row);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));
    }

    /**
     * 注册未认证列表
     */
    public function noCertList()
    {
        $n = I('page', 0);
        $num = 10;
        $page = ($n - 1) * 10;
        $where = [];
        $where['a.type'] = 3;//属于注册未认证
        if ($_SESSION['role_id'] == '2') {
            $where['a.admin_id'] = $_SESSION['id'];
        }

        $where['b.cert_status'] = 1;

        $field = 'b.mobile,b.vid,b.truename,b.ctime,b.sex';
        $count = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user b on a.vid =b.vid and b.type=3')->where($where)->count();
        $data = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user b on a.vid =b.vid and b.type=3')->where($where)->order('a.id desc')->field($field)->limit($page, $num)->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            //最近备注
            $content = M('call_info', '', 'KEFU_R')->where(['vid' => $v['vid']])->order('ctime desc')->limit(1)->field('content')->find();
            if (empty($content['content'])) {
                $data[$k]['mark'] = '--';
            } else {
                $data[$k]['mark'] = $content['content'];
            }
            if (empty($v['truename'])) {
                $data[$k]['truename'] = '---';
            }
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));
    }

    /**
     * 抢拨认证未完成
     */
    public function snatch_unfinished()
    {
        $n = I('page', 0);
        $num = 10;
        $page = ($n - 1) * 10;
        $type = I('type', 1);//1近期认证未完成 2 认证未完成
        // 一小时前 到 昨晚八点
        if ($type == '2') {
            $s_time = 1525104000;
            $end_time = strtotime(date('Y-m-d', time())) - 25200;
        } else {
            $s_time = strtotime(date('Y-m-d', time())) - 25200;
            $end_time = time() - 3600;
        }
        $where['b.ctime'] = array('between', [$s_time, $end_time]);//18-5-1
        $where['a.vid'] = array('exp', 'is null');
        $where['b.cert_status'] = array('eq', 4);
        $where['b.type'] = array('eq', 3);
        $where['b.status'] = array('eq',1);
        $field = 'b.mobile,b.vid,b.truename,b.ctime,b.sex,b.usermsg_verify,b.contact_verify,b.work_verify,b.taobao_verify,b.mobile_verify';

        $count = M('user', '', 'USER_R')->join('b left join  dxd_kefu.kf_snatch a on a.vid=b.vid and a.type=2 ')->where($where)->count();
        $data = M('user', '', 'USER_R')->join('b left join  dxd_kefu.kf_snatch a on a.vid=b.vid and a.type=2 ')->where($where)->field($field)->order('b.ctime desc')->limit($page, $num)->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            //最近备注
            $content = M('call_info', '', 'KEFU_R')->where(['vid' => $v['vid']])->order('ctime desc')->limit(1)->field('content')->find();
            if (empty($content['content'])) {
                $data[$k]['mark'] = '--';
            } else {
                $data[$k]['mark'] = $content['content'];
            }
            if (empty($v['truename'])) {
                $data[$k]['truename'] = '---';
            }
            if ($v['mobile_verify'] == '2' || $v['mobile_verify'] == '4' || $v['mobile_verify'] == '1') {
                switch ($v['mobile_verify']) {
                    case '1':
                        $data[$k]['cert_item'] = '运营商-未认证';
                        break;
                    case '2':
                        $data[$k]['cert_item'] = '运营商-认证中';
                        break;
                    case '4':
                        $data[$k]['cert_item'] = '运营商-认证失败';
                        break;
                }
            }

            if ($v['taobao_verify'] == '2' || $v['taobao_verify'] == '4') {
                switch ($v['taobao_verify']) {
                    case '1':
                        $data[$k]['cert_item'] = '淘宝-未认证';
                        break;
                    case '2':
                        $data[$k]['cert_item'] = '淘宝-认证中';
                        break;
                    case '4':
                        $data[$k]['cert_item'] = '淘宝-认证失败';
                        break;
                }
            }

            if ($v['work_verify'] == '0') {
                $data[$k]['cert_item'] = '工作认证-未认证';
            }
            if ($v['contact_verify'] == '0') {
                $data[$k]['cert_item'] = '紧急联系人-未认证';
            }

            if ($v['usermsg_verify'] == '0') {
                $data[$k]['cert_item'] = '个人信息-未认证';
            }

        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));

    }

    /**
     * 认证未完成
     */
    public function cert_unfinish()
    {
        $n = I('page', 0);
        $num = 10;
        $page = ($n - 1) * 10;
        // 一小时前 到 昨晚八点
//        $end_time = strtotime(date('Y-m-d', time())) - 14400;
//        $s_time = time() - 3600;
//        $where['a.ctime'] = array('between', [$s_time, $end_time]);//18-5-1
        if($_SESSION['role_id']=='2'){
            $where['a.admin']=$_SESSION['id'];
        }
//        $where['b.vid'] = array('exp', 'is null');
        $where['a.type'] = array('eq',5);
        $where['b.status'] = array('eq',1);
        $field = 'b.mobile,b.vid,b.truename,b.ctime,b.sex,b.usermsg_verify,b.contact_verify,b.work_verify,b.taobao_verify,b.mobile_verify';

        $count=M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user b on b.vid=a.vid and b.cert_status=4 and b.type=3')->where($where)->count();
        $data=M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user b on b.vid=a.vid and b.cert_status=4 and b.type=3' )->where($where)->field($field)->limit($page, $num)->select();
//        $count = M('user', '', 'USER_R')->join('a left join  dxd_kefu.kf_snatch b on b.vid=a.vid')->where($where)->count();
//        $data = M('user', '', 'USER_R')->join('a left join  dxd_kefu.kf_snatch b on b.vid=a.vid')->where($where)->field($field)->limit($page, $num)->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            //最近备注
            $content = M('call_info', '', 'KEFU_R')->where(['vid' => $v['vid']])->order('ctime desc')->limit(1)->field('content')->find();
            if (empty($content['content'])) {
                $data[$k]['mark'] = '--';
            } else {
                $data[$k]['mark'] = $content['content'];
            }
            if (empty($v['truename'])) {
                $data[$k]['truename'] = '---';
            }
            $data[$k]['cert_item'] = '其他-未认证';

            if ($v['mobile_verify'] == '2' || $v['mobile_verify'] == '4' || $v['mobile_verify'] == '1') {
                switch ($v['mobile_verify']) {
                    case '1':
                        $data[$k]['cert_item'] = '运营商-未认证';
                        break;
                    case '2':
                        $data[$k]['cert_item'] = '运营商-认证中';
                        break;
                    case '4':
                        $data[$k]['cert_item'] = '运营商-认证失败';
                        break;
                }
            }

            if ($v['taobao_verify'] == '2' || $v['taobao_verify'] == '4') {
                switch ($v['taobao_verify']) {
                    case '1':
                        $data[$k]['cert_item'] = '淘宝-未认证';
                        break;
                    case '2':
                        $data[$k]['cert_item'] = '淘宝-认证中';
                        break;
                    case '4':
                        $data[$k]['cert_item'] = '淘宝-认证失败';
                        break;
                }
            }

            if ($v['work_verify'] == '2' || $v['work_verify'] == '4') {
                switch ($v['work_verify']) {
                    case '0':
                        $data[$k]['cert_item'] = '工作-未认证';
                        break;
                    case '2':
                        $data[$k]['cert_item'] = '工作-认证中';
                        break;
                    case '4':
                        $data[$k]['cert_item'] = '工作-认证失败';
                        break;
                }
            }
            if ($v['contact_verify'] == '0') {
                $data[$k]['cert_item'] = '紧急联系人-未认证';
            }
            if ($v['usermsg_verify'] == '0') {
                $data[$k]['cert_item'] = '个人信息-未认证';
            }

        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));
    }


    /**
     * 回复记录 显示
     */
    public function mark()
    {
        $mobile = I('mobile');
        $type = I('type');
        switch ($type) {
            case '1';
                $data = M('user_surve_mark', '', 'KEFU')->where(['mobile' => $mobile, 'type' => 4])->field('content,admin_name,ctime')->select();
                foreach ($data as $k => $v) {
                    $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
                }
                break;
            case '2';
                $vid = M('user', '', 'USER_R')->where(['mobile' => $mobile])->getfield('vid');
                $where['ctime'] = array('egt', strtotime(date('Y-m-d', time())));
                $where['vid'] = $vid;
                $data = M('temp_today_pay_mark', '', 'KEFU')->where($where)->field('reason as content,name as admin_name,ctime')->select();
                foreach ($data as $k => $v) {
                    $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
                }

                break;
        }
        $this->assign(array('data' => $data, 'mobile' => $mobile, 'type' => $type));
        $this->display();
    }

    /**
     * 回复记录
     */
    public function add_mark()
    {
        $mobile = I('mobile');
        $info = I('info');
        $type = I('type');
        switch ($type) {
            case '1':
                //加入记录表，type==4空号调查
                M('cl_mobile_status_detail', '', 'KEFU_FK')->startTrans();
                M('user_surve_mark', '', 'KEFU')->startTrans();
                $arr = array('admin_name' => $_SESSION['name'], 'content' => $info, 'mobile' => $mobile, 'type' => 4, 'ctime' => time());
                $update = M('cl_mobile_status_detail', '', 'KEFU_FK')->where(['mobile' => $mobile])->setField('research_result', $info);
                $add = M('user_surve_mark', '', 'KEFU')->add($arr);
                if ($add && $update) {
                    M('cl_mobile_status_detail', '', 'KEFU_FK')->commit();
                    M('user_surve_mark', '', 'KEFU')->commit();
                    add_log('空号检测备注' . $add);
                    $this->ajaxReturn(array('code' => '1001', 'result' => '添加成功'));
                } else {
                    M('cl_mobile_status_detail', '', 'KEFU_FK')->rollback();
                    M('user_surve_mark', '', 'KEFU')->rollback();
                    $this->ajaxReturn(array('code' => '1002', 'result' => '添加失败'));
                }

                break;
            case 2:
                $vid = M('user', '', 'USER_R')->where(['mobile' => $mobile])->getfield('vid');
                $arr = array('vid' => $vid, 'reason' => $info, 'ctime' => time(), 'name' => $_SESSION['name']);
                $add = M('temp_today_pay_mark', '', 'KEFU')->add($arr);
                if ($add) {
                    add_log('今日需还款用户通知备注' . $add);
                    $this->ajaxReturn(array('code' => '1001', 'result' => '备注成功'));
                } else {
                    $this->ajaxReturn(array('code' => '1002', 'result' => '备注失败'));
                }
                break;
        }
    }

    /***
     * 抢播认证未借款
     */
    public function snatch_cert()
    {
        $num = '10';
        $n = I('page');
        $page = ($n - 1) * $num;
        $time = strtotime(date('Y-m-d', time()));
        $yesterday_time = $time - 7 * 3600;//昨天下班后注册用户
        $one_hour = time();//一个小时前
        $sub_sql = M('user', '', 'USER')
            ->join('a left join dxd_kefu.kf_snatch b on b.vid=a.vid and b.type=3')
            ->where(['a.cert_time' => ['between', [$yesterday_time, $one_hour]], 'a.waitdays' => ['lt', time()], 'a.type' => ['neq', 2], 'b.vid' => ['exp', 'is null'], 'a.cert_status' => 2])
            ->field('a.vid,a.truename,a.mobile,a.cert_time as ctime')
            ->select(false);
        $count = M('user', '', 'USER_R')
            ->table('(' . $sub_sql . ') a')
            ->join('left join dxd_caiwu.user_borrow b on b.vid=a.vid and b.is_success=2 and b.ctime between ' . $yesterday_time . ' and ' . time())
            ->where(['b.vid' => ['exp', 'is null']])
            ->count();

        $data = M('user', '', 'USER_R')
            ->table('(' . $sub_sql . ') a')
            ->join('left join dxd_caiwu.user_borrow b on b.vid=a.vid and b.is_success=2 and b.ctime between ' . $yesterday_time . ' and ' . time())
            ->where(['b.vid' => ['exp', 'is null']])
            ->field('a.vid,a.truename,a.mobile,a.ctime')
            ->limit($page, $num)
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            if (empty($v['truename'])) {
                $data[$k]['truename'] = '--';
            }
            //最近备注
            $mark=M('call_info','','KEFU')->where(['vid'=>$v['vid']])->order('id desc')->limit(1)->field('content,ctime,admin')->find();
            if($mark){
                $data[$k]['content']=$mark['content'];
                $data[$k]['admin']=$mark['admin'];
                $data[$k]['mark_time']=date('Y-m-d H:i:s',$mark['ctime']);
            }else{
                $data[$k]['content']='--';
                $data[$k]['mark_time']='--';
                $data[$k]['admin']='--';
            }
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));
    }

    /***
     *检查是否在通话中
     */
    public function check_call()
    {
        $mobile = I('mobile');
        $type = I('type');
        $vid = M('user', '', 'USER')->where(['mobile' => $mobile])->getfield('vid');
        $dial_status = M('kf_snatch', '', 'KEFU')->where(['vid' => $vid, 'type' => $type])->field('dial_status')->find();
        if ($dial_status['dial_status'] == '1') {
            $this->ajaxReturn(array('code' => '1001', 'result' => '通话中'));
            die;
        } else {
            $this->ajaxReturn(array('code' => '1002', 'result' => 'success'));
            die;
        }
    }

    /***
     * 点击抢拨外呼
     */
    public function click_call(){
        $mobile = I('mobile');
        $call_id = I('call_id');
        $type = I('type', 1);
        $vid = M('user', '', 'USER')->where(['mobile' => $mobile])->getfield('vid');
//        $exit=M('kf_snatch','','KEFU')->where(['vid'=>$vid])->find();
        $arr = array('vid' => $vid, 'admin' => $_SESSION['name'], 'ctime' => time(), 'type' => $type, 'dial_status' => '1', 'call_id' => $call_id);
        $add = M('kf_snatch', '', 'KEFU')->add($arr);
        $this->ajaxReturn(array('code' => '1001', 'result' => ''));
        die;

//        if(!empty($call_id)){
//            if(empty($exit)){
//                $arr=array('vid'=>$vid,'admin'=>$_SESSION['name'],'ctime'=>time(),'type'=>1,'dial_status'=>'1','call_id'=>$call_id);
//                $add=M('kf_snatch','','KEFU')->add($arr);
//            }else{
//                $arr=array('vid'=>$vid,'admin'=>$_SESSION['name'],'upd_time'=>time(),'type'=>1,'dial_status'=>'1','call_id'=>$call_id);
//                $add=M('kf_snatch','','KEFU')->where(['vid'=>$vid])->save($arr);
//            }
//            if($add){
//                $this->ajaxReturn(array('code'=>'1001','result'=>''));
//                die;
//            }
//            $this->ajaxReturn(array('code'=>'1002','result'=>''));
//            die;
//        }
    }

    /**
     * 外部导入外呼
     */
    public function outer(){
        $type=I('type',1);
        $times=I('times');
        $mark=I('mark');
        $date=I('date','0');
        $num = '10';
        $n = I('page');
        $page = ($n - 1) * $num;
        if($times){
            $where['times']=$times;
            $where1['b.times']=$times;
         }
        if($date){
            $where['ctime']=['BETWEEN',[strtotime($date),strtotime($date)+86400]];
            $where1['b.ctime']=['BETWEEN',[strtotime($date),strtotime($date)+86400]];
        }
        $where['type']=$type;

        if($mark){
            $where1['b.type']=$type;
            $sql="SELECT * from (SELECT id,calledno,content,ctime from call_info ORDER BY id desc) a group BY calledno HAVING a.content LIKE '%".$mark."%'";
            $count=M('outer_call','','KEFU')->table( '('.$sql.') a')->join('join kefu.outer_call b on b.phone=a.calledno')->where($where1)->count();
            $data=M('outer_call','','KEFU')->table( '('.$sql.') a')->join('join kefu.outer_call b on b.phone=a.calledno')->limit($page,$num)->field('phone as mobile,b.ctime')->where($where1)->order('b.id desc')->select();
        }else{
            $count=M('outer_call','','KEFU')->where($where)->count();
            $data=M('outer_call','','KEFU')->where($where)->limit($page,$num)->field('phone as mobile,ctime')->order('id desc')->select();
        }
        
        $m_time=strtotime('2019-03-1');
        //工号
        $agent=M('admin','','KEFU')->where(['agent_pwd'=>['neq','']])->getfield('agent_pwd',true);
        foreach ($data as $k=>$v){
            $data[$k]['phone']= get_mobile($v['mobile']);
            $data[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
            //最近备注
            $mark=M('call_info','','KEFU')->where(['calledno'=>$v['mobile'],'ctime'=>['gt',$m_time],'agent'=>['in',$agent]])->order('id desc')->limit(1)->field('content,ctime,admin')->find();
            if($mark){
                $data[$k]['content']=$mark['content'];
                $data[$k]['admin']=$mark['admin'];
                $data[$k]['mark_time']=date('Y-m-d H:i:s',$mark['ctime']);
            }else{
                $data[$k]['content']='--';
                $data[$k]['mark_time']='--';
                $data[$k]['admin']='--';
            }
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'count' => $count, 'tol' => $tol, 'data' => $data));
    }
}
